package online.longzipeng.cinemasys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import online.longzipeng.cinemasys.common.annotion.NeedPermissions;
import online.longzipeng.cinemasys.common.exception.AppException;
import online.longzipeng.cinemasys.common.result.ResponseEnum;
import online.longzipeng.cinemasys.common.result.Result;
import online.longzipeng.cinemasys.entity.Hall;
import online.longzipeng.cinemasys.service.HallService;
import online.longzipeng.cinemasys.vo.hall.HallQueryVO;
import online.longzipeng.cinemasys.vo.hall.HallSaveVO;
import online.longzipeng.cinemasys.vo.hall.HallUpdateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzp
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/hall")
@Api(value = "影厅相关接口",description = "影厅相关接口",tags = "影厅相关接口")
public class HallController {

    @Autowired
    HallService hallService;

    @PostMapping("/get-list-page")
    @ApiOperation("条件查询所有影厅带分页")
    public Result getListPage(@RequestBody HallQueryVO queryVO){
        IPage<Hall> hallIPage = hallService.getListPage(queryVO);
        return Result.success(hallIPage);
    }

    @GetMapping("/get-list-json")
    @ApiOperation("获取所有影厅")
    public Result getListJson(){
        return Result.success(hallService.list());
    }

    @GetMapping("/get-by-id")
    @NeedPermissions("hall:select")
    @ApiOperation("通过id查询影厅")
    public Result getById(@ApiParam(value = "影厅id",required = true)Long id){
        return Result.success(hallService.findById(id));
    }

    @PostMapping("/save")
    @NeedPermissions("hall:save")
    @ApiOperation("新增影厅")
    public Result save(@RequestBody HallSaveVO hallSaveVO){
        Hall hall = new Hall();
        BeanUtils.copyProperties(hallSaveVO,hall);
        hallService.add(hall);
        return Result.success(null,"影厅添加成功！！");
    }

    @GetMapping("/delete-one")
    @NeedPermissions("hall:delete")
    @ApiOperation("删除影厅")
    public Result deleteOne(@ApiParam(value = "影厅id",required = true)Long id){
        try {
            hallService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"影厅删除成功！！");
    }

    @GetMapping("/delete")
    @NeedPermissions("hall:delete")
    @ApiOperation("删除多个影厅")
    public Result deleteOne(@ApiParam(value = "影厅id数组",required = true)Long[] ids){
        List<Long> idList = Arrays.stream(ids).collect(Collectors.toList());
        try {
            hallService.removeByIds(idList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"影厅删除成功！！");
    }

    @PostMapping("/update")
    @NeedPermissions("hall:update")
    @ApiOperation("修改影厅")
    public Result update(@RequestBody HallUpdateVO hallUpdateVO) {
        Hall hall = new Hall();
        BeanUtils.copyProperties(hallUpdateVO,hall);
        hallService.updateHall(hall);
        return Result.success(null,"影厅修改成功！！");
    }
    
}
